package com.boot.rabbbitmqlistener.listener;


import com.boot.commons.vo.User;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "object")
public class ObjectReceiver {

    @RabbitHandler
    public void process(User user , Message message, Channel channel) {
        System.out.println("Receiver  2: 接受到 " + user);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            // int i = 1 / 0;
            channel.basicAck(deliveryTag, false);



        } catch (Exception e) {
            try {
                // 不被其他队列消费,重新入队
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

}